package com.wzf.greattruth.zookeeper.operator;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

/**
 * 删除节点操作
 * 
 * @author wuzhengfei
 *
 */
public class ZookeeperDeleteNodeOperator extends ZooKeeperOperator<String, Boolean> {

    public ZookeeperDeleteNodeOperator(ZooKeeper zookeeper) {
        super(zookeeper, null);
    }

    @Override
    public synchronized Boolean doOperate(String path) throws KeeperException, InterruptedException {
        try {
            ZooKeeper zookeeper = getZookeeper();
            zookeeper.delete(path, -1);
            return true;
        } catch (KeeperException e) {
            // 如果node不存在，直接认为删除成功
            if (e.code() == KeeperException.Code.NONODE) {
                return true;
            }
            throw e;
        }
    }
}
